using FluentNHibernate.Conventions;
using FluentNHibernate.Conventions.AcceptanceCriteria;
using FluentNHibernate.Conventions.Inspections;
using FluentNHibernate.Conventions.Instances;

namespace HashFoo.Nhib
{
    /// <summary>
    /// Automapping convention for db enums
    /// </summary>
    public class DbEnumConvention : IPropertyConvention, IPropertyConventionAcceptance
    {
        /// <summary>
        /// Apply changes to the target
        /// </summary>
        public void Apply(IPropertyInstance instance)
        {
            instance.Unique();
            instance.Not.Nullable();
            instance.Length(256);
        }

        /// <summary>
        /// Whether this convention will be applied to the target.
        /// </summary>
        /// <param name="criteria">Instace that could be supplied</param>
        /// <returns>
        /// Apply on this target?
        /// </returns>
        public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
        {
            criteria.Expect(x => typeof(DbEnum).IsAssignableFrom(x.EntityType) && x.Name == "InternalName");
        }
    }
}